<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.archived.information.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="100px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('business.information.category_id')" prop="category_id">
                    <el-select
                      v-model="dataForm.category_id"
                      :placeholder="i18n('common.please_select') + i18n('business.information.category_id')"
                      @change="loadMachineList" filterable clearable>
                      <el-option v-for="(v, k) in categoryList" :label="v.chinese_title" :key="k" :value="v.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8" v-show="is_show">
                  <el-form-item :label="i18n('business.information.machine_id')" prop="machine_id">
                    <el-select
                      v-model="dataForm.machine_id"
                      :placeholder="i18n('common.please_select') + i18n('business.information.machine_id')"
                      filterable clearable>
                      <el-option v-for="(v, k) in machineList" :label="v.chinese_title" :key="k" :value="v.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.information.title')" prop="title">
                    <el-input v-model="dataForm.title" :placeholder="i18n('common.please_input') + i18n('business.information.title')" clearable></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('business.information.shipowner_id')" prop="shipowner_id">
                    <el-select
                      v-model="dataForm.shipowner_id"
                      :placeholder="i18n('common.please_select') + i18n('business.information.shipowner_id')"
                      @change="loadShiprouteList($event),loadManagerList($event)"
                      filterable clearable>
                      <el-option v-for="(v, k) in shipownerList" :label="v.chinese_name" :key="k" :value="v.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.information.shiproute_id')" prop="shiproute_id">
                    <el-select
                      v-model="dataForm.shiproute_id"
                      :placeholder="i18n('common.please_select') + i18n('business.information.shiproute_id')" filterable clearable>
                      <el-option v-for="(v, k) in shiprouteList" :label="v.chinese_name" :key="k" :value="v.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('business.information.manager_id')" prop="manager_id">
                    <el-select
                      v-model="dataForm.manager_id"
                      :placeholder="i18n('common.please_select') + i18n('business.information.manager_id')" filterable multiple collapse-tags :multiple-limit="4" clearable>
                      <el-option v-for="(v, k) in managerList" :label="v.title" :key="k" :value="v.id"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="i18n('business.information.publish_time')" prop="publish_time">
                    <el-date-picker v-model="dataForm.publish_time" type="date" :placeholder="i18n('common.please_input') + i18n('business.information.publish_time')" clearable/>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item :label="i18n('common.status')" prop="status">
                    <el-switch
                      v-model="dataForm.status"
                      active-value="1"
                      :active-text="i18n('common.enable')"
                      inactive-value="2"
                      :inactive-text="i18n('common.disable')">
                    </el-switch>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item :label="i18n('business.information.disclaimer')" prop="description">
                    <el-input
                      type="textarea"
                      v-model="dataForm.remark.disclaimer"
                      :rows="3"
                      show-word-limit
                      maxlength="500"
                      :placeholder="i18n('common.please_input') + i18n('business.information.disclaimer')">
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="20">
                <el-col :span="24">
                  <el-form-item :label="i18n('business.information.remark')" prop="feature">
                    <el-input
                      type="textarea"
                      v-model="dataForm.remark.remark"
                      :rows="3"
                      show-word-limit
                      maxlength="500"
                      :placeholder="i18n('common.please_input') + i18n('business.information.remark')">
                    </el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('business.information.harbor_info') }}</span>
              </div>
            </template>

            <el-card class="mt10" v-for="(option, index) in dataForm.route" :key="index" shadow="never">
              <div class="text item">
                <el-row :gutter="20">
                  <el-col :span="8">
                    <el-form-item :label="i18n('business.information.harbor_code')" :prop="'route.' + index + '.harbor_code'">
                      <el-select
                        v-model="option.harbor_code"
                        :placeholder="i18n('common.please_select') + i18n('business.information.harbor_code')" :remote-method="loadHarborList" :loading="loading" reserve-keyword filterable remote clearable>
                        <el-option v-for="(v, k) in harborList" :label="v.english_name" :key="k" :value="v.code"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <!-- <el-form-item :label="i18n('business.information.price')" :prop="'route.' + index + '.price'">
                      <el-input-number v-model="option.price" :precision="2" :step="0.1" :min="0" />
                    </el-form-item> -->
                  </el-col>
                  <el-col :span="4">
                    <el-form-item :label="i18n('business.information.is_load')" :prop="'route.' + index + '.is_load'">
                      <el-switch
                        v-model="option.is_load"
                        active-value="1"
                        :active-text="i18n('common.yes')"
                        inactive-value="2"
                        :inactive-text="i18n('common.no')">
                      </el-switch>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="8">
                    <el-form-item :label="i18n('business.information.second_harbor_code')" :prop="'route.' + index + '.second_harbor_code'">
                      <el-select
                        v-model="option.second_harbor_code"
                        :placeholder="i18n('common.please_select') + i18n('business.information.second_harbor_code')" :remote-method="loadHarborList" :loading="loading" reserve-keyword filterable remote clearable>
                        <el-option v-for="(v, k) in harborList" :label="v.english_name" :key="k" :value="v.code"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item :label="i18n('business.information.third_harbor_code')" :prop="'route.' + index + '.third_harbor_code'">
                      <el-select
                        v-model="option.third_harbor_code"
                        :placeholder="i18n('common.please_select') + i18n('business.information.third_harbor_code')" :remote-method="loadHarborList" :loading="loading" reserve-keyword filterable remote clearable>
                        <el-option v-for="(v, k) in harborList" :label="v.english_name" :key="k" :value="v.code"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="8">
                    <el-form-item :label="i18n('business.information.start_time')" :prop="'route.' + index + '.start_time'">
                      <el-date-picker v-model="option.start_time" type="date" :placeholder="i18n('common.please_input') + i18n('business.information.start_time')" clearable/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="8">
                    <el-form-item :label="i18n('business.information.end_time')" :prop="'route.' + index + '.end_time'">
                      <el-date-picker v-model="option.end_time" type="date" :placeholder="i18n('common.please_input') + i18n('business.information.end_time')" clearable/>
                    </el-form-item>
                  </el-col>
                  <el-col :span="4">
                    <el-form-item :label="i18n('business.information.is_unload')" :prop="'route.' + index + '.is_unload'">
                      <el-switch
                        v-model="option.is_unload"
                        active-value="1"
                        :active-text="i18n('common.yes')"
                        inactive-value="2"
                        :inactive-text="i18n('common.no')">
                      </el-switch>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="4">
                    <el-button class="ml10 pl22" type="success" link @click.prevent="addOption()">
                      {{ i18n('common.create') }}
                    </el-button>
                    <el-button type="danger" link @click.prevent="removeOption(option)">
                      {{ i18n('common.delete') }}
                    </el-button>
                  </el-col>
                </el-row>
              </div>
            </el-card>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <div class="text item">
              <el-form-item>
                <el-button
                  type="primary"
                  @click="dataFormSubmit(formRef)">
                  {{ i18n('common.confirm') }}
                </el-button>
                <el-button @click="resetForm(formRef)">
                  {{ i18n('common.reset') }}
                </el-button>
              </el-form-item>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="business-archived-information-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { isEmpty } from '@/utils/validate'

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/archived/information')

  const is_show = ref(true)
  const loading = ref(false)
  const categoryList = ref([])
  const machineList = ref([])
  const shipownerList = ref([])
  const shiprouteList = ref([])
  const managerList = ref([])
  const harborList = ref([])
  const route_harbor_code = ref([])

  const dataForm = reactive({
    id: 0,
    category_id: '',
    machine_id: '',
    shipowner_id: '',
    shiproute_id: '',
    manager_id: [],
    title: '',
    publish_time: '',
    sort: 0,
    is_recommend: '2',
    status: '1',
    start: {
      harbor_code: '',
      start_time: '',
      end_time: '',
      is_load: '2',
      is_unload: '2',
      price: 0.00,
    },
    route: [{
      harbor_code: '',
      second_harbor_code: '',
      third_harbor_code: '',
      start_time: '',
      end_time: '',
      is_load: '2',
      is_unload: '2',
      price: 0.00,
    }],
    end: {
      harbor_code: '',
      start_time: '',
      end_time: '',
      is_load: '2',
      is_unload: '2',
      price: 0.00,
    },
    remark: {
      disclaimer: '',
      remark: '',
    }
  })

  const dataRule = reactive({
    category_id: [{
      required: true,
      message: i18n('business.information.rules.category_id.require'),
      trigger: 'blur',
    }],
    shipowner_id: [{
      required: true,
      message: i18n('business.information.rules.shipowner_id.require'),
      trigger: 'blur',
    }],
    title: [{
      required: true,
      message: i18n('business.information.rules.title.require'),
      trigger: 'blur',
    }],
    publish_time: [{
      required: true,
      message: i18n('business.information.rules.publish_time.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.shipowner_id = data.data.shipowner_id
            dataForm.shiproute_id = data.data.shiproute_id
            dataForm.category_id = data.data.category_id
            dataForm.machine_id = data.data.machine_id
            dataForm.title = data.data.title
            dataForm.publish_time = data.data.publish_time
            dataForm.sort = data.data.sort
            dataForm.status = data.data.status.value + ''

            is_show.value = dataForm.category_id == 1 ? true : false
          }
        }).then(() => {
          if (dataForm.category_id && 1 == dataForm.category_id) {
            $http({
              url: $http.adornUrl(`/business/information/machine/select`),
              method: 'get',
              params: $http.adornParams({
                category_id: dataForm.category_id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                machineList.value = data.data
              }
            })
          }
        }).then(() => {
          if (dataForm.shipowner_id) {
            $http({
              url: $http.adornUrl(`/business/shiproute/select`),
              method: 'get',
              params: $http.adornParams({
                shipowner_id: dataForm.shipowner_id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                shiprouteList.value = data.data
              }
            })
          }
        }).then(() => {
          if (dataForm.shipowner_id) {
            $http({
              url: $http.adornUrl(`/business/manager/select`),
              method: 'get',
              params: $http.adornParams({}),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                managerList.value = data.data
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/route/select`),
              method: 'get',
              params: $http.adornParams({
                information_id: dataForm.id,
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                dataForm.route = []
                data.data.forEach((res) => {
                  const temp = {}
                  temp.harbor_code = res.harbor_code
                  temp.second_harbor_code = res.second_harbor_code
                  temp.third_harbor_code = res.third_harbor_code
                  temp.start_time = res.start_time
                  temp.end_time = res.end_time
                  temp.is_load = res.is_load.value
                  temp.is_unload = res.is_unload.value
                  temp.price = parseFloat(res.price)

                  dataForm.route.push(temp)

                  route_harbor_code.value.push(res.harbor_code)

                  if(res.second_harbor_code) {
                    route_harbor_code.value.push(res.second_harbor_code)
                  }

                  if(res.third_harbor_code) {
                    route_harbor_code.value.push(res.third_harbor_code)
                  }
                })
              }
            }).then(() => {
              if (route_harbor_code) {
                $http({
                  url: $http.adornUrl('/business/harbor/select'),
                  method: 'get',
                  params: $http.adornParams({
                    id: route_harbor_code.value
                  }),
                }).then(({ data }) => {
                  if (data && data.status === 200) {
                    data.data.forEach((res) => {
                      harborList.value.push(res)
                    })
                  }
                })
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/remark/data`),
              method: 'get',
              params: $http.adornParams({
                information_id: dataForm.id
              }),
            }).then(({ data }) => {
              if (!isEmpty(data.data) && data.status === 200) {
                dataForm.remark = data.data
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/manager/select`),
              method: 'get',
              params: $http.adornParams({
                information_id: dataForm.id
              }),
            }).then(({ data }) => {
              if (!isEmpty(data.data) && data.status === 200) {
                data.data.forEach((res) => {
                  dataForm.manager_id.push(res.manager_id)
                })
              }
            })
          }
        })
      }
    })
  }

  const loadCategoryList = () => {
    $http({
      url: $http.adornUrl('/business/information/category/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({data}) => {
      if (data && data.status === 200) {
        categoryList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadMachineList = (category_id) => {
    if(1 == category_id)
    {
      $http({
        url: $http.adornUrl('/business/information/machine/select'),
        method: 'get',
        params: $http.adornParams({
          category_id: category_id,
        }),
      }).then(({data}) => {
        if (data && data.status === 200) {
          machineList.value = data.data
          dataForm.machine_id = ''
          is_show.value = true
        } else {
          $message.error(data.message)
        }
      })
    }
    else {
      dataForm.machine_id = ''
      is_show.value = false
    }
  }

  const loadShipownerList = () => {
    $http({
      url: $http.adornUrl('/business/shipowner/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        shipownerList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadShiprouteList = (shipowner_id) => {
    $http({
      url: $http.adornUrl('/business/shiproute/select'),
      method: 'get',
      params: $http.adornParams({
        shipowner_id: shipowner_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        shiprouteList.value = data.data
        dataForm.shiproute_id = ''
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadManagerList = (shipowner_id) => {
    $http({
      url: $http.adornUrl('/business/manager/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        managerList.value = data.data
        dataForm.manager_id = []
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadHarborList = (name) => {
    if (name && name.length > 1) {
      loading.value = true
      $http({
        url: $http.adornUrl('/business/harbor/select'),
        method: 'get',
        params: $http.adornParams({
          keyword: name
        }),
      }).then(({ data }) => {
        if (data && data.status === 200) {
          harborList.value = data.data
          loading.value = false
        } else {
          $message.error(data.message)
        }
      })
    } else {
      harborList.value = []
    }
  }

  const addOption = () => {
    // 校验
    if (dataForm.route.length > 0) {
      let option = dataForm.route[dataForm.route.length - 1]
      if (isEmpty(option.harbor_code)) {
        $message.error(i18n('business.information.placeholder_input_option'));
        return
      }

      dataForm.route.push({ harbor_code: '', second_harbor_code: '', third_harbor_code: '', start_time: '', end_time: '', is_load: '2',  is_unload: '2', price: 0.00 })
    } else {
      dataForm.route.push({ harbor_code: '', second_harbor_code: '', third_harbor_code: '', start_time: '', end_time: '', is_load: '2',  is_unload: '2', price: 0.00 })
    }
  }

  const removeOption = (item) => {
    let index = dataForm.route.indexOf(item)
    if (index !== -1) {
      dataForm.route.splice(index, 1)
    }
  }

  onMounted(() => {
    init(formRef)

    loadCategoryList()
    loadShipownerList()
  })
</script>

<style scoped lang="scss">
  .pl22 {
    padding-left: 22px;
  }
</style>
